var table_option = {
	idField: 'id',
	pagination: false,
	showRefresh: false,
	search: false,
	// queryParams: function (query) {
	// query['category'] = category;
	// query['exclude'] = exclude;
	//     return query;
	// },
	// url: ''
};
$('#bootstrap-votedata-table-container').bootstrapTable(table_option);
function votesNumberFormatter(value, row) {
	return (row.votes_number - row.intervention_number);
}
function votesSumFormatter(value, row) {
	if(row.votes_number < 0 ){
		return 0;
	}
	return row.votes_number;
}
function imgFormatter(value, row) {
	var str = '';
	if (row.smpicture != '') {
		str = '<img src="' + row.cover_url + '" class="img-responsive"/>'
	}
	return str;
}
function statusFormatter(value, row) {
	var str = '<span class="text-warning">未通过</span>';
	if (row.isCheck == 1) {
		str = '<span class="text-warning">通过</span>';
		return str;
	}
	return str;
}
function operateFormatter(value, row, index) {
	return '<span class="btn btn-simple btn-info btn-icon table-action intervene" item-id="' + value + '">人工干预</span>';
	// return '<span class="text-warning">导出投票记录</span>';
}
window.operateEvents = {
	'click .text-warning': function () {

	},
	'click .intervene': function () {
		var interveneUrl = '/web/vote/intervene';
		var itemID = $(this).attr('item-id');
		$.get(interveneUrl, {'item_id': itemID}, function (data, status) {
			var number = 0;
            var votesNumber = 0;

            if (status == 'success' && data.code == 200) {
					number = data.data;
                    votesNumber = data.votes_number;
				}
				var modalContent = '<input type="number" class="form-control" placeholder="请输入干预票数" value="' + number + '">';
            		modalContent += '<input type="hidden" id="votesNumber" value="' + votesNumber + '">';
                    modalContent += '<input type="hidden" id="interventionNumber" value="' + number + '">';

            var modalObj = utils.modal('人工干预', modalContent, false, function (result) {
					if (result === 1) {
						var interventionNumber = modalObj.find('input').val();
                        var inVotesNumber = $('#interventionNumber').val();
                        var votesNumber = $('#votesNumber').val();

                        var minInterventionNumber = -1 * (parseInt(votesNumber) - parseInt(inVotesNumber));

                        if (interventionNumber == '') {
							utils.showNotification('请输入干预票数', 3);
							return;
						}
						if (isNaN(interventionNumber)) {
							utils.showNotification('干预票数只能输入整数', 3);
							return;
						}
                        if (interventionNumber < minInterventionNumber) {
                            utils.showNotification('干预票数不能小于'+minInterventionNumber, 3);
                            return;
                        }
						$.post(interveneUrl, {
							'item_id': itemID,
							'intervention_number': interventionNumber
						}, function (data, status) {
							if (status == 'success') {
								if (data.code === 200) {
									modalObj.on('hidden.bs.modal', function () {
										$('#bootstrap-votedata-table-container').bootstrapTable('refresh');
									})
									modalObj.modal('hide');
								} else {
									utils.showNotification(data.message, 3);
								}
							} else {
								console.log(data);
								utils.showNotification(data, 3);
							}
						});
					} else {
						modalObj.modal('hide');
					}
				});
			}
		)
		;
	}
};
$('.vote-back-list').click(function () {
	window.location.href = '/web/activity/index';
});